<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 16:19
 */
namespace app\data;

use app\common\Common;
use think\Db;

class ShopData extends Base
{
    /**
     * @param $user_id
     * @param $shop_name
     * @param $shop_logo
     * @param $shop_introduce
     * @param $type_id
     * @param $shop_type
     * @param $img
     * @param $subtitle
     * @param $tel
     * @param $shop_user_name
     * @param $shop_we_chat
     * @return array
     */
    public function addShop($user_id, $shop_name, $shop_logo, $shop_introduce, $type_id,
                            $shop_type, $img, $subtitle, $tel, $shop_user_name, $shop_we_chat)
    {
        $time = time();
        $id = Db::table('drh_shop')
            ->insertGetId([
                'user_id' => $user_id,
                'shop_name' => $shop_name,
                'shop_logo' => $shop_logo,
                'shop_introduce' => $shop_introduce,
                'type_id' => $type_id,
                'shop_type' => $shop_type,
                'img' => $img,
                'subtitle' => $subtitle,
                'create_time' => $time,
                'shop_no' => $tel,
                'shop_user_name' => $shop_user_name,
                'shop_we_chat' => $shop_we_chat,
            ]);

        return ['code' => $this->t_code, 'msg' => '添加成功', 'data' => $id];
    }

    /**
     * @param $user_id
     * @param $shop_id
     * @param $shop_name
     * @param $shop_logo
     * @param $shop_introduce
     * @param $type_id
     * @param $shop_type
     * @param $img
     * @param $subtitle
     * @param $tel
     * @param $shop_user_name
     * @param $shop_we_chat
     * @return array
     */
    public function editShop($user_id, $shop_id, $shop_name, $shop_logo, $shop_introduce,
                             $type_id, $shop_type, $img, $subtitle, $tel, $shop_user_name, $shop_we_chat)
    {
        Db::table('drh_shop')
            ->where([
                'user_id' => $user_id,
                'id' => $shop_id,
            ])
            ->update([
                'shop_name' => $shop_name,
                'shop_logo' => $shop_logo,
                'shop_introduce' => $shop_introduce,
                'type_id' => $type_id,
                'shop_type' => $shop_type,
                'img' => $img,
                'subtitle' => $subtitle,
                'verify_status' => 0,
                'shop_no' => $tel,
                'shop_user_name' => $shop_user_name,
                'shop_we_chat' => $shop_we_chat,
            ]);

        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * @param $user_id
     * @param $shop_id
     * @return array
     */
    public function delShop($user_id, $shop_id)
    {
        Db::table('drh_shop')
            ->where([
                'user_id' => $user_id,
                'id' => $shop_id,
            ])
            ->update([
                'recover_status' => 1,
            ]);

        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function shopInfo($shop_id)
    {
        return Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field([
                'b.city_id',
                'b.province_id',
                'b.district_id',
                'b.address',
                'a.shop_name',
                'a.shop_logo',
                'a.shop_no',
                'a.subtitle',
                'a.shop_introduce',
                'a.type_id',
                'a.type_lv',
                'a.shop_type',
                'a.img',
                'a.money',
                'a.integral'
            ])
            ->where(['id' => $shop_id])
            ->find();
    }

    public function getAllComment($shop_id)
    {
        $res = Db::table('drh_user_comment')
            ->field([
                'AVG(comment_integral) comment_integral',
            ])->where([
                'shop_id' => $shop_id
            ])->find();
        return empty($res) ? 100 : $res['comment_integral'];
    }

    public function showList($page, $pre, $sort, $recover_status = 0)
    {
//        $sort = Common::spliceArray($sort);
//        $sort .= "create_time desc";

        return Common::db_count("id,shop_name,shop_logo","","drh_shop","recover_status = {$recover_status} and verify_status =1 and shop_type < 2 limit {$page}, {$pre}");
//        return Db::table('drh_shop')
//            ->field([
//                'id',
//                'shop_name',
//                'shop_logo',
//            ])->where(
//                "recover_status = {$recover_status} and verify_status =1 and shop_type < 2"
//            )
//            ->order($sort)
//            ->limit($page, $pre)
//            ->select();
    }

    /**
     * 获取数量
     * @param $where
     * @param $table
     * @return mixed
     */
    public function total($where, $table = 'drh_shop')
    {
        $res = Db::table($table)
          ->field('count(id) as count')
            ->where($where)
            ->find();

        return $res['count'];
    }

    public function showType($sort)
    {
        $sort = Common::spliceArray($sort);

        return Db::table('drh_shop_type')
            ->field([
                'id',
                'type_name',
                'type_img',
            ])
            ->order($sort)
            ->select();
    }

    public function changeShopHot($shop_id, $hot_lv)
    {
        Db::table('drh_shop')
            ->where([
                'shop_id' => $shop_id
            ])->update([
                'hot_lv' => $hot_lv
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * 展示分类商户
     * @param $type_id
     * @param $page
     * @param $pre
     * @param $sort
     * @param int $recover_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showTypeShop($type_id, $page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);

        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'shop_logo',
                'shop_introduce'
            ])->where([
                'recover_status' => $recover_status,
                'verify_status' => 1,
                'type_id' => $type_id,
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 添加商品类别
     * @param $shop_id
     * @param $type_name
     * @param $lv
     * @param $parent_type_id
     * @return array
     */
    public function addGoodsType($shop_id, $type_name, $lv, $parent_type_id = 0)
    {
        $time = time();
        Db::table('drh_goods_type')
            ->insert([
                'type_name' => $type_name,
                'shop_id' => $shop_id,
                'lv' => $lv,
                'parent_type_id' => $parent_type_id,
                'create_time' => $time
            ]);
        return ['code' => $this->t_code, 'msg' => '分类添加成功'];
    }

    /**
     * 修改商品类别
     * @param $shop_id
     * @param $type_name
     * @param $type_id
     * @param $lv
     * @param $parent_type_id
     * @return array
     */
    public function editGoodsType($shop_id, $type_name, $lv, $type_id, $parent_type_id = 0)
    {
        Db::table('drh_goods_type')
            ->where([
                'shop_id' => $shop_id,
                'id' => $type_id,
            ])
            ->update([
                'type_name' => $type_name,
                'lv' => $lv,
                'parent_type_id' => $parent_type_id,
            ]);
        return ['code' => $this->t_code, 'msg' => '分类修改成功'];
    }

    /**
     * 删除商品类别
     * @param $shop_id
     * @param $type_id
     * @return array
     */
    public function delGoodsType($shop_id, $type_id)
    {
        Db::table('drh_goods_type')
            ->where([
                'shop_id' => $shop_id,
                'id' => $type_id
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    /**
     * 添加商品轮播图
     * @param $shop_id
     * @param $goods_id
     * @param $img
     * @return array
     */
    public function addTypeRotation($shop_id, $goods_id, $img)
    {
        $time = time();
        Db::table('drh_shop_carousel_img')
            ->insert([
                'shop_id' => $shop_id,
                'goods_id' => $goods_id,
                'create_time' => $time,
                'img' => $img,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function delTypeRotation($shop_id, $id)
    {
        Db::table('drh_shop_carousel_img')
            ->where([
                'id' => $id,
                'shop_id' => $shop_id
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function showTypeRotation($shop_id)
    {
        $res = Db::table('drh_shop_carousel_img a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'a.id',
                'a.shop_id',
                'a.goods_id',
                'a.img',
                'b.goods_name',
            ])->where([
                'a.shop_id' => $shop_id
            ])->select();
        return ['code' => $this->t_code, 'msg' => 'OK', 'data' => $res];
    }

    /**
     * 校验商品
     * @param $shop_id
     * @param $goods_id
     * @return bool
     */
    public function checkGoods($shop_id, $goods_id)
    {
        $res = Db::table('drh_goods')
            ->field([
                'id'
            ])->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ])->find();
        return empty($res) ? false : true;
    }

    /**
     * 显示商品分类
     * @param $shop_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showGoodsType($shop_id)
    {
        $res = Db::table('drh_goods_type')
            ->field([
                'id',
                'type_name',
            ])->where(" shop_id in (0,{$shop_id})")
            ->select();
        return ['code' => $this->t_code, 'msg' => 'OK', 'data' => $res];
    }

    /**
     * 显示商品列表
     * @param $shop_id
     * @param $page
     * @param $pre
     * @param $sort
     * @param $is_shelves
     * @param $type_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showGoodsList($shop_id, $page, $pre, $sort, $is_shelves, $type_id)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods')
            ->field([
                'id',
                'goods_name',
                'goods_price',
                'goods_img',
                'goods_attr_value',
                'goods_info'
            ])->where([
                'is_shelves' => $is_shelves,
                'shop_id' => $shop_id,
                'goods_type_id' => $type_id,
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }
    public function showGoodsList_agency($shop_id, $page, $pre, $sort, $is_shelves, $type_id)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods a')
            ->join('drh_goods_proxy b','a.id=b.goods_id','left')
            ->field([
                'a.id',
                'a.goods_name',
                'a.goods_price',
                'a.goods_img',
                'a.goods_attr_value',
                'a.lightspot'
            ])->where([
                'a.is_shelves' => $is_shelves,
                'b.shop_id' => $shop_id,
                'b.goods_type_id' => $type_id,
                'b.is_not'=>0
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 显示商品详情
     * @param $goods_id
     * @param $is_shelves
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function showGoods($goods_id, $is_shelves = 1)
    {
        return Db::table('drh_goods')
            ->field([
                'id',
                'shop_id',
                'goods_name',
                'goods_price',
                'give_integral',
                'goods_freight',
                'goods_stock',
                'goods_info',
                'goods_attr_value',
                'goods_img'
            ])->where([
                'id' => $goods_id,
                'is_shelves' => $is_shelves
            ])->find();
    }

    /**
     * 添加商品
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param $goods_group
     * @param $is_shelves
     * @param $goods_stock
     * @param $goods_hot
     * @param $goods_new
     * @param $goods_attr_value
     * @param $goods_info
     * @param $goods_img
     * @param $is_proxy
     * @param $goods_freight
     * @param $proxy_goods_value
     * @return array
     */
    public function addGoods($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                             $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                             $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy, $proxy_goods_value)
    {
        $time = time();
        $shelves_time = $is_shelves == 1 ? $time : 0;
        $id = Db::table('drh_goods')
            ->insertGetId([
                'shop_id' => $shop_id,
                'goods_name' => $goods_name,
                'goods_price' => $goods_price,
                'give_integral' => $give_integral,
                'goods_type_id' => $goods_type_id,
                'goods_group' => $goods_group,
                'is_shelves' => $is_shelves,
                'goods_stock' => $goods_stock,
                'goods_hot' => $goods_hot,
                'goods_new' => $goods_new,
                'goods_freight' => $goods_freight,
                'goods_attr_value' => $goods_attr_value,
                'goods_info' => $goods_info,
                'goods_img' => $goods_img,
                'shelves_time' => $shelves_time,
                'is_proxy' => $is_proxy,
                'create_time' => $time,
                'proxy_goods_value' => $proxy_goods_value,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功', 'id' => $id];
    }

    public function addGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status, $game_status, $time)
    {
        return Db::table('drh_goods_plugin')
            ->insertGetId([
                'goods_id' => $goods_id,
                'distribution_status' => $distribution_status,
                'bargain_status' => $bargain_status,
                'group_buy_status' => $group_buy_status,
                'game_status' => $game_status,
                'create_time' => $time
            ]);
    }

    public function editGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status, $game_status)
    {
        return Db::table('drh_goods_plugin')
            ->where([
                'goods_id' => $goods_id,
            ])->update([
                'distribution_status' => $distribution_status,
                'bargain_status' => $bargain_status,
                'group_buy_status' => $group_buy_status,
                'game_status' => $game_status,
            ]);
    }

    /**
     * 修改商品
     * @param $id
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param $goods_group
     * @param $is_shelves
     * @param $goods_stock
     * @param $goods_hot
     * @param $goods_new
     * @param $goods_attr_value
     * @param $goods_info
     * @param $goods_img
     * @param $goods_freight
     * @param $is_proxy
     * @param $proxy_goods_value
     * @return array
     */
    public function editGoods($id, $shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                              $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                              $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy, $proxy_goods_value)
    {
        $time = time();
        $shelves_time = $is_shelves == 1 ? $time : 0;
        Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $id
            ])->update([
                'goods_name' => $goods_name,
                'goods_price' => $goods_price,
                'give_integral' => $give_integral,
                'goods_type_id' => $goods_type_id,
                'goods_group' => $goods_group,
                'is_shelves' => $is_shelves,
                'goods_stock' => $goods_stock,
                'goods_hot' => $goods_hot,
                'goods_new' => $goods_new,
                'goods_attr_value' => $goods_attr_value,
                'goods_info' => $goods_info,
                'goods_img' => $goods_img,
                'shelves_time' => $shelves_time,
                'create_time' => $time,
                'goods_freight' => $goods_freight,
                'is_proxy' => $is_proxy,
                'proxy_goods_value' => $proxy_goods_value,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * 删除商品
     * @param $id
     * @param $shop_id
     * @return array
     */
    public function delGoods($id, $shop_id)
    {
        $time = time();
        Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $id
            ])->update([
                'is_shelves' => 3,
                'off_shelves_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function showAdminGoodsList($shop_id, $page, $pre, $sort, $is_shelves)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return  Db::table('drh_goods a')
            ->join('drh_goods_type b', 'a.goods_type_id=b.id', 'left')
            ->join('drh_goods_plugin c', 'a.id=c.goods_id', 'left')
            ->field([
                'a.id',
                'a.shop_id',
                'a.goods_name',
                'a.goods_price',
                'a.give_integral',
                'a.goods_group',
                'a.is_shelves',
                'a.proxy_goods_id',
                'a.goods_stock',
                'a.proxy_goods_value',
                'a.goods_attr_value',
                'a.goods_img',
                'a.goods_info',
                'b.type_name',
                'IFNULL(c.distribution_status,1) distribution_status',
                'IFNULL(c.bargain_status,1) bargain_status',
                'IFNULL(c.group_buy_status,1) group_buy_status',
                'IFNULL(c.game_status,1) game_status',
            ])->where([
                'a.shop_id' => $shop_id,
                'a.is_shelves' => $is_shelves
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }
    public function showAdminGoodsList_two($shop_id, $page, $pre, $sort, $is_shelves)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
       return Db::table('drh_goods a')
            ->join('drh_goods_proxy t','t.shop_id=a.id','left')
            ->join('drh_goods_type b', 't.goods_type_id=b.id', 'left')
            ->join('drh_goods_plugin c', 'a.id=c.goods_id', 'left')
            ->field([
                'a.id',
                'a.rebates',
                'a.lightspot',
                'a.shop_id',
                'a.goods_name',
                'a.goods_price',
                'a.give_integral',
                'a.goods_group',
                'a.is_shelves',
                'a.proxy_goods_id',
                'a.goods_stock',
                'a.proxy_goods_value',
                'a.goods_attr_value',
                'a.goods_img',
                'a.goods_info',
                'b.id as type_id',
                'b.type_name',
                'IFNULL(c.distribution_status,1) distribution_status',
                'IFNULL(c.bargain_status,1) bargain_status',
                'IFNULL(c.group_buy_status,1) group_buy_status',
                'IFNULL(c.game_status,1) game_status',
            ])
           ->where('a.is_shelves', $is_shelves)
           ->where('is_ware',1)
           ->where('a.id','in',$shop_id)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function changeGoodsType($shop_id, $goods_id, $type_id)
    {
        return Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ])->update([
                'goods_type_id' => $type_id
            ]);
    }

    public function showAdminGoodsInfo($shop_id, $goods_id)
    {
        return Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ])->find();
    }

    public function createBargainGoods($shop_id, $goods_id, $bargain_total_money,
                                       $bargain_once_money, $title, $goods_attr_value)
    {
        $time = time();
        Db::table('drh_goods_bargain')
            ->insert([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
                'bargain_total_money' => $bargain_total_money,
                'bargain_once_money' => $bargain_once_money,
                'title' => $title,
                'create_time' => $time,
                'goods_attr' => $goods_attr_value
            ]);
        return ['code' => $this->t_code, 'msg' => '创建成功'];
    }

    public function editBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money,
                                     $title, $goods_attr_value)
    {
        Db::table('drh_goods_bargain')
            ->where([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
                'goods_value' => $goods_attr_value
            ])
            ->update([
                'bargain_total_money' => $bargain_total_money,
                'bargain_once_money' => $bargain_once_money,
                'title' => $title,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function delBargainGoods($shop_id, $goods_id)
    {
        Db::table('drh_goods_bargain')
            ->where([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function showBargainGoods($shop_id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, ' a.');
        return Db::table('drh_goods_bargain a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'b.goods_name',
                'a.bargain_total_money',
                'a.bargain_once_money',
                'a.title',
                'a.shop_id',
                'a.goods_id',
                'a.goods_value'
            ])->where([
                'a.shop_id' => $shop_id
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showBargainGood($where)
    {

        return Db::table('drh_goods_bargain')
            ->field([
                'bargain_total_money',
                'bargain_once_money',
                'title',
                'shop_id',
                'goods_id',
                'goods_attr',
            ])->where($where)->find();
    }

    public function addProxyGoods($shop_id, $info)
    {
        $time = time();
        $shelves_time = $time;
        Db::table('drh_goods')
            ->insert([
                'shop_id' => $shop_id,
                'goods_name' => $info['goods_name'],
                'goods_price' => $info['goods_price'],
                'give_integral' => $info['give_integral'],
                'goods_type_id' => $info['goods_type_id'],
                'goods_group' => $info['goods_group'],
                'is_shelves' => $info['is_shelves'],
                'goods_stock' => $info['goods_stock'],
                'goods_hot' => $info['goods_hot'],
                'goods_new' => $info['goods_new'],
                'goods_freight' => $info['goods_freight'],
                'goods_attr_value' => $info['goods_attr_value'],
                'goods_info' => $info['goods_info'],
                'goods_img' => $info['goods_img'],
                'shelves_time' => $shelves_time,
                'is_proxy' => 0,
                'proxy_goods_id' => $info['id'],
                'create_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function showProxyShop($page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, ' a.');
        return Db::table('drh_shop a')
            ->join('drh_goods b', 'a.id=b.shop_id', 'left')
            ->field([
                'a.id',
                'a.shop_name'
            ])->where("b.is_proxy=1 and a.verify_status=1 and a.recover_status=0")
            ->group('a.id')
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showShopTotal()
    {
        $res = Db::table('drh_shop a')
            ->join('drh_goods b', 'a.id=b.shop_id', 'left')
            ->field([
                'count(*)'
            ])->where("b.is_proxy=1")
            ->group('a.id')
            ->select();

        return empty($res) ? 0 : count($res);
    }

    public function showShopProxyGoods($shop_id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, ' a.');

        return Db::table('drh_goods a')
            ->join('drh_goods_plugin b', 'a.id=b.goods_id', 'left')
            ->field([
                'a.id',
                'a.goods_name'
            ])->where("a.is_proxy = 1 and a.shop_id={$shop_id} and a.is_shelves=1 and b.distribution_status !=0")
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showShopProxyGoodsTotal($shop_id)
    {
        $res = Db::table('drh_goods')
            ->field([
                'count(*) count'
            ])->where("is_proxy=1 and shop_id={$shop_id}")
            ->find();
        return empty($res) ? 0 : $res['count'];
    }

    public function cancelGoodsProxy($goods_id)
    {
        Db::table('drh_goods')
            ->where([
                'proxy_goods_id' => $goods_id
            ])->update([
                'is_shelves' => 3
            ]);
    }

    public function editProxyGoods($id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                                   $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                                   $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy)
    {
        $time = time();
        $shelves_time = $is_shelves == 1 ? $time : 0;
        Db::table('drh_goods')
            ->where([
                'proxy_goods_id' => $id
            ])->update([
                'goods_name' => $goods_name,
                'goods_price' => $goods_price,
                'give_integral' => $give_integral,
                'goods_type_id' => $goods_type_id,
                'goods_group' => $goods_group,
                'goods_stock' => $goods_stock,
                'goods_hot' => $goods_hot,
                'goods_new' => $goods_new,
                'goods_attr_value' => $goods_attr_value,
                'goods_info' => $goods_info,
                'goods_img' => $goods_img,
                'shelves_time' => $shelves_time,
                'create_time' => $time,
                'goods_freight' => $goods_freight,
                'is_proxy' => $is_proxy,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }


    public function getBargainGoodInfo($id)
    {
        return Db::table('drh_user_goods_bargain')
            ->field([
                'id',
                'bargain_total_money',
                'bargain_once_money',
                'title',
                'shop_id',
                'goods_id',
                'bargain_total_num',
                'bargain_num',
            ])->where([
                'id' => $id
            ])->find();
    }

    public function shopFlow($order_num, $time, $user_info, $type_status, $money_expenses, $integral_expenses,
                             $fees, $msg = '', $give_integral = 0)
    {
        Db::table('drh_user_flow')
            ->insert([
                'user_id' => $user_info['id'],
                'start_money' => $user_info['user_money'],
                'start_integral' => $user_info['user_integral'],
                'order_num' => $order_num,
                'type_status' => $type_status,
                'money_expenses' => $money_expenses,
                'integral_expenses' => $integral_expenses,
                'end_money' => $user_info['user_money'] + $money_expenses,
                'end_integral' => $user_info['user_integral'] + $integral_expenses - $give_integral,
                'create_time' => $time,
                'fees' => $fees,
                'msg' => $msg,
                'return_integral' => '-' . $give_integral,
            ]);
        return true;
    }

    public function getShopInfo($id)
    {
        return Db::table('drh_shop')
            ->field([
                'id',
                'user_id',
                'money',
                'integral',
                'shop_name',
            ])->where([
                'id' => $id
            ])->find();
    }

    public function endOrder($id, $money, $integral)
    {


         Db::table('drh_user_info')
            ->where([
                'id' => $id
            ])->update([
                'user_money' => $money,
                'user_integral' => $integral
            ]);

        return true;
    }

    public function endGoods($shop_id, $goods_num, $goods_id)
    {
        Db::table('drh_goods')
            ->field(" goods_stock=goods_stock-{$goods_num} ")
            ->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ]);
        Db::table('drh_goods')
            ->field("goods_stock=goods_stock-{$goods_num} ")
            ->where([
                'proxy_goods_id' => $goods_id
            ]);
    }

    public function showAdminShop($status, $page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
            ])->where([
                'verify_status' => $status,
                'recover_status' => $recover_status,
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function verifyShop($shop_id, $status)
    {
        Db::table('drh_shop')
            ->where([
                'id' => $shop_id,
            ])->update([
                'verify_status' => $status
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function showWekOrder($shop_id)
    {
        $time = time();
        $time_start = date('Y-m-d', $time - 7 * 24 * 60 * 60);
        $time_end = date('Y-m-d', $time);
        return Db::table('calendar a')
            ->field([
                'a.date_list',
                "(select count(*) 
                from 
                drh_goods_order 
                where 
                FROM_UNIXTIME(order_end_time,'%Y-%m-%d') = a.date_list and shop_id={$shop_id}) 
                count"
            ])->group('a.date_list')
            ->where(" a.date_list >= '{$time_start}'  and a.date_list<='{$time_end}'")
            ->select();
    }

    public function showGoodsNumber($shop_id, $page = 1, $pre = 15, $is_shelves = 1)
    {
        $page = ($page - 1) * $pre;
        return Db::table('drh_goods a')
            ->field([
                'a.id',
                'a.goods_name',
                '( select count(*) from drh_goods_order where goods_id=a.id ) count',
                'a.goods_img'
            ])->where([
                'a.shop_id' => $shop_id,
                'a.is_shelves' => $is_shelves
            ])->group('a.id')
            ->order('count desc')
            ->limit($page, $pre)
            ->select();
    }

    public function showAnnexShop($page, $pre, $sort, $city_name, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field([
                'a.id',
                'a.shop_name',
                'a.shop_logo',
            ])->where([
                'a.recover_status' => $recover_status,
                'a.verify_status' => 1,
                'b.city' => $city_name,
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showGoodNumber()
    {

    }

    public function searchShop($name, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'shop_logo',
                'shop_introduce',
            ])->where(" shop_name like '%{$name}%' and verify_status=1 and recover_status=0")
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }


    public function addBankCard($user_id, $user_name, $bank_name, $time, $bank_card_name, $card_number)
    {
        Db::table('drh_bank_card')
            ->insert([
                'user_id' => $user_id,
                'user_name' => $user_name,
                'bank_name' => $bank_name,
                'create_time' => $time,
                'card_number' => $card_number,
                'card_name' => $bank_card_name
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function editBankCard($user_id, $user_name, $bank_name, $bank_card_name, $card_number)
    {
        Db::table('drh_bank_card')
            ->where([
                'user_id' => $user_id
            ])
            ->update([
                'card_number' => $card_number,
                'user_name' => $user_name,
                'bank_name' => $bank_name,
                'card_name' => $bank_card_name
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function showBankCardInfo($user_id)
    {
        return Db::table('drh_bank_card')
            ->field([
                'card_number',
                'user_name',
                'bank_name'
            ])->where([
                'user_id' => $user_id
            ])->find();
    }

    public function getAnnexShopNumber($city_name)
    {
        $res = Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field('count(*) count')
            ->where([
                'b.city' => $city_name,
                'a.verify_status' => 1,
                'a.recover_status' => 0,
            ])->find();
        return $res['count'];
    }

    public function showShop($page, $pre, $sort, $verify_status)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'verify_status'
            ])->where([
                'verify_status' => $verify_status
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showAdminShopInfo($id)
    {
        return Db::table('drh_shop')
            ->field([
                'shop_name',
                'shop_no',
                'recover_status',
                'integral',
                'money'
            ])->where([
                'id' => $id
            ])->find();
    }

    public function showProxyGoods($id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods')
            ->field([
                'goods_name',
                'id'
            ])->where("proxy_goods_id > 0 and shop_id={$id}")
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function addShopCity($shop_id, $city, $province, $city_id, $province_id,
                                $district_id, $district_name, $address)
    {
        Db::table('drh_shop_city')
            ->insert([
                'shop_id' => $shop_id,
                'city' => $city,
                'province' => $province,
                'city_id' => $city_id,
                'province_id' => $province_id,
                'district_id' => $district_id,
                'district_name' => $district_name,
                'address' => $address,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function editShopCity($shop_id, $city, $province, $city_id, $province_id,
                                 $district_id, $district_name, $address)
    {
        Db::table('drh_shop_city')
            ->where([
                'shop_id' => $shop_id,
            ])
            ->update([
                'city' => $city,
                'province' => $province,
                'city_id' => $city_id,
                'province_id' => $province_id,
                'district_id' => $district_id,
                'district_name' => $district_name,
                'address' => $address,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function getInfo($user_open_id, $value, $whereName)
    {
        return Db::table('drh_shop a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field($value)
            ->where([
                $whereName => $user_open_id
            ])->find();
    }

    public function changIntegralMoney($shop_id, $shop_integral, $shop_money)
    {
        Db::table('drh_shop')
            ->where([
                'id' => $shop_id
            ])->update([
                'integral' => $shop_integral,
                'money' => $shop_money,
            ]);
        return true;
    }

    public function getWithdrawUserInfo($order_num)
    {
        return Db::table('drh_withdraw a')
            ->join('drh_shop b', 'a.user_id=b.user_id', 'left')
            ->field([
                'b.integral',
                'b.money',
                'b.id',
                'a.total_money',
                'a.user_id',
                'a.review_type'
            ])->where([
                'order_num' => $order_num
            ])->find();
    }

    public function showAllGoodsList($shop_id)
    {
        $proxy = Common::db_count("id,goods_name,is_ware","","drh_goods","is_ware=1 AND id IN(".implode(",",Common::er_array(Common::db_count("goods_id","","drh_goods_proxy","shop_id=".$shop_id))).")");
        $goods = Db::table('drh_goods')
            ->field([
                'id',
                'goods_name',
                'is_ware'
            ])->where([
                'shop_id' => $shop_id
            ])->select();
        return array_merge($goods,$proxy);
    }
    public function combine($shop_id){
        $proxy = Common::db_count_id("COUNT(id)","`count`","drh_goods","is_ware=1 AND id IN(".implode(",",Common::er_array(Common::db_count("goods_id","","drh_goods_proxy","shop_id=".$shop_id))).")");
        $goods = Db::table('drh_goods')
            ->field([
                'id',
                'goods_name'
            ])->where([
                'shop_id' => $shop_id
            ])->count();
        return $proxy+$goods;
    }
    public function getFriendShipShop($page, $pre, $sort, $user_id)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_friend a')
            ->join('drh_shop b', 'a.friend_id=b.user_id', 'left')
            ->join('drh_user_info c', 'b.user_id=c.id', 'left')
            ->field([
                'b.shop_logo',
                'b.shop_name',
                'b.id shop_id',
            ])->where([
                'a.user_id' => $user_id,
                'c.recover_status' => 0,
                'b.verify_status' => 1,
                'b.recover_status' => 0
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function getFriendShipShopTotal($user_id)
    {
        $res = Db::table('drh_user_friend a')
            ->join('drh_shop b', 'a.friend_id=b.user_id', 'left')
            ->join('drh_user_info c', 'b.user_id=c.id', 'left')
            ->field([
                'count(*) count'
            ])->where([
                'a.user_id' => $user_id,
                'c.recover_status' => 0,
                'b.verify_status' => 1,
                'b.recover_status' => 0
            ])->find();
        return empty($res) ? 0 : $res['count'];
    }

    public function searchGoods_prox($name, $page, $pre, $goods_num,$desc)
    {
        $page = ($page - 1) * $pre;
        $array = Db::table('drh_goods a')
            ->join('drh_shop c', 'a.shop_id=c.id', 'left')
            ->join('drh_goods_order b', 'a.id=b.goods_id', 'left')
            ->join('drh_user_comment d','b.order_num=d.order_num','left')
            ->field([
                'a.id goods_id',
                'a.goods_name',
                'a.goods_attr_value',
                'a.goods_price goods_price',
                'IFNULL(sum(b.goods_num),0) goods_num',
                'a.goods_img',
                'a.shop_id',
                'c.shop_name'
            ])->where("a.goods_name like '%{$name}%' and 
             a.is_shelves=1")
            ->group("a.id")
            ->order($goods_num,$desc)
            ->limit($page, $pre)
            ->select();
        foreach ($array as $k => $v) {
            if($v['shop_name'] == null){
                $shop_id = Common::er_array(Common::db_count('shop_id','',"drh_goods_proxy","is_not=0 and goods_id={$v['goods_id']}"));
                if($shop_id !== array()){
                    $shop_id = implode(',',$shop_id);
                    $shop = Common::er_array(Common::db_count('id,shop_name',"","drh_shop","id IN ({$shop_id}) ORDER BY type_lv limit 1"));
                    $array[$k]['shop_id'] = !empty($shop[0])?$shop[0]:null;
                    $array[$k]['shop_name'] = !empty($shop[1])?$shop[1]:null;
                }
            }
            $goods_order = Db::query("SELECT goods_num,order_num,order_rating FROM drh_goods_order WHERE goods_id={$v['goods_id']}");
            $he = Common::summation($goods_order,'order_rating');
            $count = count($goods_order);
            $array[$k]['order_rating'] = $count==0?0:bcdiv($he,$count);
        }
       return $array;
    }
    public function searchGoods($name, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods a')
            ->join('drh_goods_order b', 'a.id=b.goods_id', 'left')
            ->join('drh_shop c', 'a.shop_id=c.id', 'left')
            ->join('drh_user_comment d','b.order_num=d.order_num','left')
            ->field([
                'a.id goods_id',
                'a.goods_name',
                'a.goods_attr_value',
                'a.goods_price goods_price',
                'IFNULL(AVG(d.comment_integral),0) order_rating',
                'IFNULL(sum(b.goods_num),0) goods_num',
                'a.goods_img',
                'a.shop_id',
                'c.shop_name'
            ])->where("
            a.goods_name like '%{$name}%' 
            and 
             a.is_shelves=1 
            and 
             c.verify_status=1 
            and 
             c.recover_status=0  
            ")->order($sort)
            ->group("a.id")
            ->limit($page, $pre)
            ->select();
    }

    public function searchGoodsTotal($name)
    {
        $res = Db::table('drh_goods a')
            ->join('drh_shop c', 'a.shop_id=c.id', 'left')
            ->field([
                'count(*) count',
            ])->where("
            a.goods_name like '%{$name}%' 
            and 
             a.is_shelves=1 
            and 
             c.verify_status=1 
            and 
             c.recover_status=0  
            ")
            ->find();

        return empty($res) ? 0 : $res['count'];
    }

    public function getShopInfoInfo($shop_id)
    {
        return Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field([
                'a.shop_name',
                'a.id shop_id',
                'b.province',
                'b.city',
                'b.district_name',
                'b.address'
            ])->where([
                'a.id' => $shop_id
            ])->find();
    }

    public function showCommentOrder($where, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_comment a')
            ->join('drh_goods_order b', 'a.order_num=b.order_num', 'left')
            ->join('drh_user_info c', 'a.user_id=c.id', 'left')
            ->field([
                'c.user_head_img',
                'c.user_name',
                'a.comment_integral',
                'a.msg',
                'a.img',
                "FROM_UNIXTIME(a.create_time,'%Y-%m-%d') time"
            ])->where($where)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function getCommentTotal($where)
    {
        $res = Db::table('drh_user_comment a')
            ->join('drh_goods_order b', 'a.order_num=b.order_num', 'left')
            ->field([
                'count(*) count'
            ])->where($where)
            ->find();
        return empty($res) ? 0 : $res['count'];
    }

    public function addFriendShop($shop_id, $add_shop_id, $time)
    {
        return Db::table('drh_friend_shop')
            ->insertGetId([
                'shop_id' => $shop_id,
                'add_shop_id' => $add_shop_id,
                'create_time' => $time
            ]);
    }

    public function editFriendShop($shop_id, $add_shop_id)
    {
        return Db::table('drh_friend_shop')
            ->where([
                'shop_id' => $shop_id,
                'add_shop_id' => $add_shop_id,
            ])->delete();
    }

    public function showFriendShop($shop_id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_friend_shop a')
            ->join('drh_shop b', 'a.add_shop_id=b.id', 'left')
            ->field([
                'b.shop_name',
                'b.shop_logo',
                'b.id shop_id',
                'b.shop_introduce',
                'b.shop_type'
            ])->where([
                'a.shop_id' => $shop_id
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showShopPlugin($shop_id)
    {
        return Db::table('drh_shop_plugin')
            ->field([
                'shop_id',
                'distribution_status',
                'bargain_status',
                'group_buy_status',
                'game_status'
            ])->where([
                'shop_id' => $shop_id
            ])->find();
    }

    public function addShopPlugin($shop_id, $distribution_status, $bargain_status, $group_buy_status_id, $game_status, $time)
    {
        return Db::table('drh_shop_plugin')
            ->insertGetId([
                'shop_id' => $shop_id,
                'distribution_status' => $distribution_status,
                'bargain_status' => $bargain_status,
                'group_buy_status' => $group_buy_status_id,
                'game_status' => $game_status,
                'create_time' => $time
            ]);
    }

    public function editShopPlugin($shop_id, $distribution_status, $bargain_status, $group_buy_status_id, $game_status)
    {
        return Db::table('drh_shop_plugin')
            ->where([
                'shop_id' => $shop_id
            ])->update([
                'distribution_status' => $distribution_status,
                'bargain_status' => $bargain_status,
                'group_buy_status' => $group_buy_status_id,
                'game_status' => $game_status,
            ]);
    }

    public function showShopUser($shop_id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_attention_shop a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field([
                'b.id user_id',
                'b.user_name'
            ])->where([
                'a.shop_id' => $shop_id,
                'b.recover_status' => 0
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function getShopUserTotal($shop_id)
    {
        $res = Db::table('drh_user_attention_shop a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field([
                'count(*) count'
            ])->where([
                'a.shop_id' => $shop_id,
                'b.recover_status' => 0
            ])->find();
        return empty($res['count']) ? 0 : $res['count'];
    }

    public function getShopTel($shop_id)
    {
        $res =Db::table('drh_shop a')
            ->join('drh_user_info b','a.user_id=b.id','left')
            ->field([
                'b.user_tel'
            ])->where([
                'a.id'=>$shop_id
            ])->find();
        return empty($res['user_tel'])?'':$res['user_tel'];
    }
}